<?php
class Admin_ProductCategoryController extends Apps_Controller_Action_Admin {
    public function readAction() {

        $select = $this->_dbAdapter
                       ->select()
                       ->from(array('p_c_1' => 'product_category'), null)
                       ->joinLeft(array('p_c_2' => 'product_category'), 'p_c_2.id = p_c_1.parent_id', null)
                       ->where('p_c_1.status !=?' , Model_DbTable_ProductCategory::STATUS_DELETED)
                       ->columns(array('id'                   => 'p_c_1.id',
                                       'product_title'        => 'p_c_1.title',
                                       'product_parent_title' => 'p_c_2.title',
                                       'status'               => 'p_c_1.status',
                                       'status_label'         => new Zend_Db_Expr('CASE p_c_1.status WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
                                       'sorting_number'       => 'p_c_1.sorting_number'));

        $grid = new Grid_Product_Category('product-category-grid');
        $grid->setCaption('Product Category List');
        $grid->setSource(new OeZend_Widget_Grid_Source_ZendDbSelect($select));
        $grid->setView(new OeZend_Widget_Grid_View());
        $grid->setDisablePaginator();

        $grid->deploy();

        $this->view->grid = $grid;
    }

    public function createAction() {
        $rq = $this->_request;

        $tbProductCategory = new Model_DbTable_ProductCategory();
        $f 	= new Admin_Form_AddProductCategory();

        if ($rq->isPost()) {
            if ($f->isValid($rq->getParams())) {
                $this->_flashMessenger->addMessage(
                        $tbProductCategory->insert($f->getValues())
                        ? self::$msgAddSuccess
                        : self::$msgAddFailed);

                $this->_helper->redirector('read', null, null);
            }
        }
        $f->setLegend('Add New Product Category');
        $this->view->form = $f;
    }

    public function updateAction() {
        $rq = $this->_request;
        $id = $rq->getParam('id');

        $tbProductCategory = new Model_DbTable_ProductCategory();

        if(!$id || !is_numeric($id) || !$tbProductCategory->fetchRow(array('id =?' => $id))) {
            $this->_helper->redirector('read', null, null);
        }

        $f 	= new Admin_Form_AddProductCategory();
        if ($rq->isPost()) {
            if ($f->isValid($rq->getParams())) {
                $this->_flashMessenger->addMessage(
                        $tbProductCategory->update($f->getValues(), array('id =?' => $id))
                        ? self::$msgUpdateSuccess
                        : self::$msgUpdateFailed);
                $this->_helper->redirector('read', null, null);
            }
        }

        $f->setLegend('Update Product Category');
        $f->populate($tbProductCategory->fetchRow(array('id =?' => $id))->toArray());
        $this->view->form = $f;
        $this->render('create');
    }

    //DELETE
    public function deleteAction() {
        $this->disableLayout(true, true);

        $id = $this->_getParam('id')
              ? $this->_getParam('id')
              : $this->_getParam('item_checker');

        if (!$id) {
            $this->_helper->redirector('read', null, null);
        }

        $tbProductCategory = new Model_DbTable_ProductCategory();
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (is_numeric($v)) {
                    $tbProductCategory->update(array('status' => $tbProductCategory::STATUS_DELETED),
                            array('id =?'  => $v));
                }
            }
        } else if (is_numeric($id)) {
            if ($tbProductCategory->update(array('status' => $tbProductCategory::STATUS_DELETED),
                    array('id =?'  => $id)))
            {
                $this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
            }
        }
        $this->_helper->redirector('read', null, null);
    }

    public function changeValueAction() {
        $this->disableLayout(true, true);
        $rq = $this->_request;
        $param  = $rq->getParam('p');
        $id 	= intval($rq->getParam('id'));
        $tbProductCategory = new Model_DbTable_ProductCategory();
        if(!($r = $tbProductCategory->fetchRow(array('id = ?' => $id))->toArray())) {
            echo '<script>window.location=window.location</script>
            <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
            . $this->getBacktrackUrl()
            . '">Update Error (Record not found), please wait ...</a>';
        }

        switch ($param) {
            case 'show_home':
                ($r['show_home'] == Model_DbTable_ProductCategory::SHOW_HOME)
                ? $tbProductCategory->safelyUpdate(array('show_home' 		=> Model_DbTable_ProductCategory::NOT_SHOW_HOME),
                array('id =?'			=> $id))
                : $tbProductCategory->safelyUpdate(array('show_home' 		=> Model_DbTable_ProductCategory::SHOW_HOME),
                array('id =?'			=> $id));
                break;

            case 'public':
                ($r['status'] == Model_DbTable_ProductCategory::STATUS_ACTIVE)
                ? $tbProductCategory->safelyUpdate(array('status'  => Model_DbTable_ProductCategory::STATUS_INACTIVE),
                array('id =?'	  => $id))
                : $tbProductCategory->safelyUpdate(array('status'  => Model_DbTable_ProductCategory::STATUS_ACTIVE),
                array('id =?'  => $id));
                break;


            default: break;
        }

        echo '<script>document.location.reload()</script>
        <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
        . $this->getBacktrackUrl()
        . '">Update successfully, please wait ...</a>';
    }
}